<?php

namespace addons\example\utils;

use app\common\model\platform\PlatformApp;
use EasyWeChat\Factory;

class MinApp
{
    public $app;
    public function __construct()
    {
        $this->app = $this->getApp();
    }

    /**
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \EasyWeChat\MiniProgram\Application|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getApp()
    {
        if($this->app){
            return $this->app;
        }
        $platformApp = (new PlatformApp())->where(['id' => get_appid()])->find();
        $server_config = $platformApp['server_config'] ?? [];
        $config = [
            'app_id' => $server_config['appid'],
            'secret' => $server_config['secret'],
        ];
        $this->app = Factory::miniProgram($config);
        return $this->app;
    }
}
